package com.yihen.mappers;

import com.yihen.dto.OrderDTO;
import com.yihen.pojo.Order;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface OrderMapper {
    // 生成订单
    @Insert("insert into tb_orders values (#{orderId},#{userId},#{datetime},#{status},#{appraise})")
    public int insertOrder(Order order);

    // 生成订单-商品
    @Insert("insert into tb_lineitem values (#{goodsId},#{orderId})")
    public int insertOrderGoods(Long goodsId, Long orderId);


    // 获取个人购买订单
    public List<OrderDTO> queryMyBuyOrders(Long userId);

    // 获取个人卖出订单
    public List<OrderDTO> queryMySellOrders(Long userId);

    // 获取全部订单
    public List<OrderDTO> queryAllOrders();

    @Update("update tb_orders set status=#{status},appraise=#{appraise} where order_id=#{orderId} and user_id=#{userId}")
    // 修改订单状态
    public int updateOrderStatusAndAppraise(Long userId,Long orderId, String status,int appraise);

    // 重置评价
    @Update("update tb_orders set appraise=0 where order_id=#{orderId}")
    public int updateOrderStatusTo0(Long orderId);
    // 通过订单号查找商品id
    @Select("select goods_id from tb_lineitem where order_id=#{orderId}")
    public Long queryGoodsIdByOrderId(Long orderId);

    // 取消订单
    @Delete("delete from tb_orders where order_id=#{orderId}")
    public int deleteOrderByOrderId(Long orderId);

    // 取消订单-商品
    @Delete("delete from tb_lineitem where order_id=#{orderId}")
    public int deleteOrderGoodsByOrderId(Long orderId);

    // 根据订单id查询订单(卖出)
    public List<OrderDTO> querySellOrdersByOrderId(Long orderId, Long userId);

    // 根据订单id查询订单(买入)
    public List<OrderDTO> queryBuyOrdersByOrderId(Long orderId,Long userId);

    // 根据订单id查询订单(管理员)
    public List<OrderDTO> queryOrdersByOrderId(Long orderId);

}
